﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web_Oto.Business;
using Web_Oto.Common;

namespace Web_Oto.UC
{
    public partial class ProductSlide : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
                GetProductsSlide();
        }

        private void GetProductsSlide()
        {
            var productBus = new ProductBus();
            var showProductSlide = int.Parse(ConfigurationManager.AppSettings["ShowProductSlide"]);
            var isHomepage = false;
            var isPayment = false;
            var isService = false;
            var isNews = false;
            var currentUrl = Request.Url.AbsolutePath.Split(new char[] { '/' })[1];
            switch (currentUrl)
            {
                case "tin-tuc":
                    isNews = true;
                    break;
                case "trang-chu":
                    isHomepage = true;
                    break;
                case "phuong-thuc-thanh-toan":
                    isPayment = true;
                    break;
                case "dich-vu-tu-van":
                    isService = true;
                    break;
            }
            var productList = productBus.GetProductForSlide(showProductSlide,isHomepage,isNews,isService,isPayment);
            if(productList != null)
            {
                var sb = new StringBuilder();
                //foreach (var product in productList)
                //{
                //    sb.Append("<li>");
                //    sb.Append("<div class='boxgrid caption'>");
                //    sb.AppendFormat("<a href='/san-pham/{2}/{3}/{4}/{5}'><img src='/images/products/{0}' alt='{1}' /></a>",
                //        product.Image, product.TitleOfImage, CommonHelper.NiceUrl(product.CategoryParentName), 
                //        CommonHelper.NiceUrl(product.CategoryName), CommonHelper.NiceUrl(product.Name), product.Id);
                //    sb.AppendFormat("<div class='cover boxcaption'><h3>{0}</h3>", product.Name);
                //    sb.AppendFormat("<p>{0}<br /><a href='/san-pham/{1}/{2}/{3}-{4}.html' class='viewdetail'>Chi tiết</a></p>",
                //        product.Name, CommonHelper.NiceUrl(product.CategoryParentName), CommonHelper.NiceUrl(product.CategoryName), CommonHelper.NiceUrl(product.Name), product.Id);
                //    sb.Append("</div></div></li>");
                //}
                var path = ConfigurationManager.AppSettings["productPath"];
                foreach (var product in productList)
                {
                    sb.AppendFormat("<li><a href='/san-pham/{0}/{1}/{2}-{3}.html' class='mask_home'><div></div><img src='{4}/{5}' title='{6}' alt='{6}'></a>",
                        CommonHelper.NiceUrl(product.CategoryParentName.ToLower()), CommonHelper.NiceUrl(product.CategoryName.ToLower()),
                        CommonHelper.NiceUrl(product.Name.ToLower()), product.Id, path, product.Image, product.TitleOfImage);
                    sb.AppendFormat("<div><h5><a href='/san-pham/{0}/{1}/{2}-{3}.html' class='product-name'>{4}</a></h5></div>", CommonHelper.NiceUrl(product.CategoryParentName.ToLower()), CommonHelper.NiceUrl(product.CategoryName.ToLower()),
                        CommonHelper.NiceUrl(product.Name.ToLower()), product.Id, product.Name);
                    sb.Append("</li>");
                }
                ltrSlideProducts.Text = sb.ToString();
            }
        }
    }
}